package com.woniuxy.producer;

import com.woniuxy.util.RabbitMQConst;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class DirectProducer implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnsCallback{
    @Resource
    private RabbitTemplate rabbitTemplate;
public void sendDeleteUserOrder(String msg,String routingKey){
        rabbitTemplate.convertAndSend(RabbitMQConst.DELETE_USER_ORDER_EXCHANGE,routingKey,msg);
    }

    /**
     * 加入购物车
     *
     */
    public void sendAddShoppingCart(String msg,String routingKey){
        rabbitTemplate.convertAndSend(RabbitMQConst.ADD_SHOPPING_CART_EXCHANGE,routingKey,msg);
    }
    public void sendAddShoppingCartByProductId(String msg,String routingKey){
        rabbitTemplate.convertAndSend(RabbitMQConst.ADD_SHOPPING_CART_EXCHANGE,routingKey,msg);
    }
    @Override
    public void confirm(CorrelationData correlationData, boolean b, String s) {
        System.out.println(correlationData);
        System.out.println(b);
        System.out.println(s);
    }
    @Override
    public void returnedMessage(ReturnedMessage returned) {
        System.out.println(returned.getMessage());//消息内容
        System.out.println(returned.getReplyCode());//失败代码
        System.out.println(returned.getReplyText());//失败原因
        System.out.println(returned.getExchange());//交换机名字
        System.out.println(returned.getRoutingKey());//路由key

    }
}
